package com.sihai.ehr.service.impl;

import java.text.MessageFormat;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.ExamAward;
import com.sihai.ehr.model.biz.ExamAwardAuditing;
import com.sihai.ehr.repository.AuditingRepository;
import com.sihai.ehr.service.ExamAwardService;

/**
 * 考核奖惩服务实现
 * 
 * @author henry
 * 
 */
@Service("examAwardService")
public class ExamAwardServiceImpl implements ExamAwardService {
	@Resource
	DataAccessor dataAccessor;

	@Resource
	private AuditingRepository auditingRepository;

	@Override
	public ExamAward getExamAward(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(ExamAward.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取考核奖惩明细数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：ExamAwardServiceImpl.getExamAward;参数id=" + id
					+ " 】");
		}
	}

	@Override
	public LimitedList<ExamAward> searchExamAward(final Long organizationId,
			final int awardType, Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(ExamAward.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamAward e where cancel=0");
							dynamicQuery
									.append(
											" and e.employeeinfoWork.organization.treePath like '%," + organizationId + ",%'");
							if (awardType >= 0) {
								dynamicQuery.appendIfNotNull(
										" and e.awardType =:awardType",
										"awardType", awardType);
							}
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardServiceImpl.searchExamAward;参数organizationId="
							+ organizationId + "】");
		}
	}
	
	@Override
	public LimitedList<ExamAward> searchExamAward(final Long organizationId,
			final int awardType,final int year,final int month, Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(ExamAward.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamAward e where cancel=0");
							dynamicQuery
									.append(
											" and e.employeeinfoWork.organization.treePath like '%," + organizationId + ",%'");
							if (awardType >= 0) {
								dynamicQuery.appendIfNotNull(
										" and e.awardType =:awardType",
										"awardType", awardType);
							}
							//年
							if (year > 0) {
								dynamicQuery.appendIfNotNull(
										" and YEAR(e.awardDate) =:year",
										"year", year);
							}
							//月
							if (month > 0) {
								dynamicQuery.appendIfNotNull(
										" and MONTH(e.awardDate) =:month",
										"month", month);
							}
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardServiceImpl.searchExamAward;参数organizationId="
							+ organizationId + "】");
		}
	}
	
	@Override
	public LimitedList<ExamAward> searchExamAwardByWorkId(final Long workId,
			Limit limit, Sorter sorter) {
		if (workId == null || workId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(ExamAward.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamAward e where cancel=0");
							dynamicQuery
									.append(
											" and e.employeeinfoWork.id =" + workId);
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardServiceImpl.searchExamAwardByWorkId;参数workId="
							+ workId + "】");
		}
	}
	
	@Override
	public LimitedList<ExamAward> searchExamAwardByWorkIdMonth(final Long workId,
			final int year, final int month, Limit limit, Sorter sorter) {
		if (workId == null || workId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(ExamAward.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamAward e where cancel=0");
							dynamicQuery
									.append(
											" and e.employeeinfoWork.id =" + workId);
							//年
							if (year > 0) {
								dynamicQuery.appendIfNotNull(
										" and YEAR(e.awardDate) =:year",
										"year", year);
							}
							//月
							if (month > 0) {
								dynamicQuery.appendIfNotNull(
										" and MONTH(e.awardDate) =:month",
										"month", month);
							}
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核奖惩时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamAwardServiceImpl.searchExamAwardByWorkId;参数workId="
							+ workId + "】");
		}
	}

	@Override
	public void saveExamAward(ExamAward examAward) {
		if (examAward == null || examAward.getEmployeeinfoWork() == null) {
			throw new RuntimeException("【examAward对象参数异常】");
		}
		boolean isNew = examAward.getId() != null && examAward.getId() > 0 ? false
				: true;
		try {
			dataAccessor.saveOrUpdate(examAward);
			if (isNew) {
				saveAwardAuditingProcess(examAward, 1);
			}
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培考核奖惩对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：ExamAwardServiceImpl.saveExamAward】");
		}
	}

	/**
	 * 创建审核流程
	 * 
	 * @param examAward
	 * @param auditNo
	 */
	@Override
	public void saveAwardAuditingProcess(ExamAward examAward, int auditNo) {
		try {
			auditingRepository.saveAuditingProcess(examAward, auditNo);
		} catch (Exception ex) {
			//
		}
	}

	@Override
	public void removeExamAward(final String examAwards) {
		if (examAwards == null || examAwards.isEmpty()) {
			throw new RuntimeException("【参数异常：examAward为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("update ExamAward e set cancel = 1 where e.id in ({0})",
									examAwards));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移出考核奖惩信息数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：ExamAwardServiceImpl.saveExamAward】");
		}
	}

	@Override
	public List<List<ExamAwardAuditing>> searchExamAwardAuditings(Long awardId,
			Limit limit, Sorter sorter) {
		return auditingRepository.findByHql(ExamAwardAuditing.class,
				"from ExamAwardAuditing i where i.examAward.id = ?",
				new Object[] { awardId });
	}
}
